package com.hzw.core.version

import android.os.Build
import com.hzw.core.ctx.appCtx
import com.hzw.core.log.logE

fun getPackageName(): String = appCtx.packageName

fun getAppVersionName(): String {
    return getAppVersionName(getPackageName())
}

fun getAppVersionName(packageName: String): String {
    return if (packageName.isBlank()) {
        ""
    } else {
        try {
            val pi = appCtx.packageManager.getPackageInfo(packageName, 0)
            pi.versionName ?: ""
        } catch (e: Exception) {
            e.logE()
            ""
        }
    }
}

fun getAppVersionCode(): Long {
    return getAppVersionCode(getPackageName())
}

fun getAppVersionCode(packageName: String): Long {
    return if (packageName.isBlank()) {
        -1L
    } else {
        try {
            val pi = appCtx.packageManager.getPackageInfo(packageName, 0)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                pi.longVersionCode
            } else {
                pi.versionCode.toLong()
            }
        } catch (e: Exception) {
            e.logE()
            -1L
        }
    }
}

fun getAppName(packageName: String): String {
    return if (packageName.isBlank()) {
        ""
    } else {
        try {
            val pm = appCtx.packageManager
            val pi = pm.getPackageInfo(packageName, 0)
            pi.applicationInfo.loadLabel(pm).toString()
        } catch (e: Exception) {
            e.logE()
            ""
        }
    }
}



